package de.grosjohann.android.feuerwehr;

import java.io.Serializable;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.grosjohann.android.feuerwehr.domain.FireIndex;
import de.grosjohann.android.feuerwehr.domain.WeatherWarning;
import de.grosjohann.android.feuerwehr.type.FireIndexType;
import de.grosjohann.android.feuerwehr.type.WeatherWarningType;

public class WeatherDetailActivity
  extends Activity {

  public static final String WEATHER_OBJECT = "feuerwehr.WEATHER_OBJECT";

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.weatherdetail);

    ((TextView) findViewById(R.id.tvHeader)).setTextSize(20);
  }

  @Override
  protected void onResume() {
    super.onResume();

    Bundle b = getIntent().getExtras();
    if (b != null) {
      Serializable serObj = b.getSerializable(WEATHER_OBJECT);
      if (serObj instanceof WeatherWarning) {
        updateWeatherWarningDetail((WeatherWarning) serObj);
      } else if (serObj instanceof FireIndex) {
        updateFireIndexDetail((FireIndex) serObj);
      }
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.weather, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case R.id.miWeatherInfo:
        startActivity(new Intent(this, WeatherInfoActivity.class));
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void updateWeatherWarningDetail(WeatherWarning ww) {

    WeatherWarningType level = ww.getLevel();

    ((TextView) findViewById(R.id.tvHeader)).setText(level.getResId());

    TextView tvWarningMessage = (TextView) findViewById(R.id.tvWarningMsg);
    tvWarningMessage.setText(ww.getMessage());

    TextView tvWarningEnd = (TextView) findViewById(R.id.tvWarningEnd);
    if (ww.getEnd() == null) {
      tvWarningEnd.setVisibility(View.GONE);
    } else {
      String msg = getString(R.string.lblWarningEndPrefix) + " " + ww.getEnd();
      tvWarningEnd.setVisibility(View.VISIBLE);
      tvWarningEnd.setText(msg);
    }

    TextView tvWarningLevelMarker = (TextView) findViewById(R.id.tvWarningLevelMarker);
    tvWarningLevelMarker.setBackgroundColor(ww.getLevel().getColor());

    TextView tvWarningDetail = (TextView) findViewById(R.id.tvWarningDetail);
    tvWarningDetail.setText(ww.getDetail());

    // Beschreibung bzgl. Wetterwarnstufen
    LinearLayout ll = (LinearLayout) findViewById(R.id.llLevelDesc);
    for (WeatherWarningType index : WeatherWarningType.values()) {
      View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.weatherlevel_item, null, false);
      ((TextView) view.findViewById(R.id.tvLevelColor)).setBackgroundColor(index.getColor());
      ((TextView) view.findViewById(R.id.tvLevelDesc)).setText(index.getResId());
      ((TextView) view.findViewById(R.id.tvLevelDesc)).setTextColor(index.getColor());
      ll.addView(view);
    }
  }

  private void updateFireIndexDetail(FireIndex fi) {
    ((TextView) findViewById(R.id.tvHeader)).setText(R.string.lblWeatherWarning);

    TextView tvWarningMessage = (TextView) findViewById(R.id.tvWarningMsg);
    tvWarningMessage.setText(R.string.lblFireIndex);

    TextView tvWarningEnd = (TextView) findViewById(R.id.tvWarningEnd);
    tvWarningEnd.setVisibility(View.GONE);

    TextView tvWarningLevelMarker = (TextView) findViewById(R.id.tvWarningLevelMarker);
    tvWarningLevelMarker.setBackgroundColor(fi.getLevel().getColor());

    TextView tvWarningDetail = (TextView) findViewById(R.id.tvWarningDetail);
    tvWarningDetail.setText(fi.getLevel().getResId());

    // Beschreibung bzgl. Waldbrandindexstufen
    LinearLayout ll = (LinearLayout) findViewById(R.id.llLevelDesc);
    for (FireIndexType index : FireIndexType.values()) {
      View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.weatherlevel_item, null, false);
      ((TextView) view.findViewById(R.id.tvLevelColor)).setBackgroundColor(index.getColor());
      ((TextView) view.findViewById(R.id.tvLevelDesc)).setText(index.getResId());
      ((TextView) view.findViewById(R.id.tvLevelDesc)).setTextColor(index.getColor());
      ll.addView(view);
    }
  }
}
